/**
 * 购物车模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	require("../../custom/base/component-core.js");
	
	var base = require("../../custom/base/component-base.js");
	
	var async = require("../../custom/base/component-async.js");
	
	var validate= require("../../custom/base/component-validate.js");
		
	//初始化函数
	exports.init=function(m){
				
		window.$ldh_cart=m;
		
		exports.getCartNum();

	};
	
	//设置商家代码
	exports.setShopId=function(shopId){
		
		this.shopId=shopId;
		
	};
	
	//获取商家代码
	exports.getShopId=function(){
		
		return this.shopId;
		
	};
	
	
	
	//查询购物车数量
	exports.getCartNum=function(){
			var params = {
					shopId:exports.getShopId()
			};
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/getCartShopNum.do", params));

								if(data["code"]==1){
									$("#cartNum").html(data["nums"]);
									$("#price").html(data["prices"].toFixed(2));
								}else{
									$("#cartNum").html("0.00");
								}
								
			}))().start();
	};
	
    
	//添加商品到购物车
    var addFlag=true;
	exports.addCartNum=function(obj,type,shopId,merchandiseId,price,position){
		
		var userId=$("#session_userid").val();
		
		if(validate.isNull(userId)){
			base.loginShow();
			return;
		}
		
		if(!addFlag)
			return;
		
		addFlag=false;
		
		var params = {
				shopId:shopId,
				merchandiseId:merchandiseId
			};
		
		$(obj).attr("disabled","disabled");
		
		if(type==0){
			$(obj).css("background-image","url("+ctx+"/v1/img/btn_loading.gif)");
		}else{
			$(obj).html("请稍候...");
		}
		
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/addshoptoCart.do", params));

							if(data["code"]==1){
								$("#cartNum").html(parseInt($("#cartNum").text())+1);
								$("#price").html((Number($("#price").text())+Number(price)).toFixed(2));
								$("#hasBuy_"+merchandiseId).show();
								$("#sub_"+merchandiseId).show();
								$("#cartId"+position).val(data["cartId"]);
								var showNum= $("#hasBuy_"+merchandiseId+" span:eq(0)").text();
								
								if(showNum==""){
									
									$("#hasBuy_"+merchandiseId+" span:eq(0)").text("1");
									
								}else{
									
									$("#hasBuy_"+merchandiseId+" span:eq(0)").text(parseInt(showNum)+1);
									
									
								}
								
								if(type==0){
									
									var cart_x=$(".ldh-cart-box").offset().left,
									cart_y=92,
									product_x=$(obj).parents(".ldh-goods-box").find(".ldh-gb-top").offset().left,		
									product_y=$(obj).parents(".ldh-goods-box").find(".ldh-gb-top").offset().top-base.getMyScrollTop(),
							        a=$(obj).parents(".ldh-goods-box").find(".ldh-gb-top").html(),
									pic_box='<div class="ldh-body-box"><div class="ldh-pic-box">'+a+'</div></div>';
									$("body").append(pic_box);
									if($(".ldh-body-box")){
										$(".ldh-pic-box").css("left",product_x);
										$(".ldh-pic-box").css("top",product_y);	
										$(".ldh-pic-box").css("width",234);	
										$(".ldh-pic-box").css("height",155);		
										$(".ldh-pic-box").animate({left:cart_x,width:"40px",height:"25px",top:cart_y},600);
										$(".ldh-pic-box").fadeTo(100,0.3);
										setTimeout(function(){$(".ldh-body-box").remove()},750);	
									}
									
								}
					
								

								//如果购物车时显示的状态,那么在添加商品的时候,刷新购物车列表,实时更新
								if($("#ldh-cart").hasClass("on")){
									exports.getCartList(shopId);
								}
								
							}else{
								
								alert(data["message"]);
							}
							

							$(obj).attr("disabled",false);
							
							if(type==0){
								
								$(obj).css("background-image","url("+ctx+"/v1/img/add_icon.png)");

								$(obj).removeAttr("style");
								
							}else{
								
								$(obj).html("加入购物车");
								$("#tip_add_cartnum").show();
								task("#tip_add_cartnum").start();
							}
							
						
							addFlag=true;
							
							isMinAmount(false);
							
							
		}))().start();
		
	};
	
	//定时提示信息
	var tips_task = eval(Wind.compile("async", function (divId) {
		
	    $await(Wind.Async.sleep(2000));
	   
	    $(divId).hide();
	    
	    
	}));
	
	var task = eval(Wind.compile("async", function (divId) {
	    $await(tips_task(divId));
	}));
	//从购物车删除商品
	var deleteNum=true;
	exports.deleteCartNum=function(obj,merchandiseId,price,position){
		
			if(!deleteNum)
				return;
		
			deleteNum=false;
			
			var nums= parseInt($("#hasBuy_"+merchandiseId+" span:eq(0)").text());
		
			nums=nums-1;
		
			
			var params = {
				cartId:$("#cartId"+position).val(),
				nums:nums
			};
			
			$(obj).attr("disabled","disabled");
			
			$(obj).css("background-image","url("+ctx+"/v1/img/btn_loading.gif)");
		
			
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/updateCartShopsNum.do", params));

							if(data["code"]==1){
								
								$("#cartNum").html(parseInt($("#cartNum").text())-1);
								
								$("#price").html((Number($("#price").text())-Number(price)).toFixed(2));
								
								$("#hasBuy_"+merchandiseId+" span:eq(0)").text(nums);
																
								$(obj).attr("disabled",false);
								
								$(obj).css("background-image","url("+ctx+"/v1/img/sub_icon.png)");
								
								$(obj).removeAttr("style");
								
								if(nums==0){
									$("#hasBuy_"+merchandiseId).hide();
									$("#sub_"+merchandiseId).hide();
								}
							}else{
								
								$(obj).attr("disabled",false);
								
								$(obj).css("background-image","url("+ctx+"/v1/img/sub_icon.png)");
								
							}
							
							//如果购物车时显示的状态,那么在添加商品的时候,刷新购物车列表,实时更新
							if($("#ldh-cart").hasClass("on")){
								exports.getCartList(exports.getShopId());
							}
							
							deleteNum=true;
							
							isMinAmount(false);
							
		}))().start();
		
	};
	
	
	//更新购物车数量
	
	var updateFlag=true;
	
	exports.updateCartNum=function(type,cartId,merchandiseId,price,position,position_i){
		
		if(!updateFlag)
			return;
		
		
		
		
		var nums=parseInt($("#merNum"+position).text());
		
		if(type=="add"){
			
			nums=nums+1;
			
		}else{
			
			nums=nums-1;
			
			if(nums<0)
			{
				//alert("购买数量必须大于1哦");
				return;
			}
			
			
		}
		
		updateFlag=false;
		
		
		var params = {
			cartId:cartId,
			nums:nums
		};
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/updateCartShopsNum.do", params));

							if(data["code"]==1){
								
								if(type=="add"){
									
									$("#cartNum").html(parseInt($("#cartNum").text())+1);
									
									$("#price").html((Number($("#price").text())+Number(price)).toFixed(2));
									
									$("#merNum"+position).html(parseInt($("#merNum"+position).text())+1);
									
									$("#hasBuy_"+merchandiseId+" span:eq(0)").text($("#merNum"+position).text());
									
									if($("#merNum"+position).parent().parent().parent().find(".ldh-checkbox-span").hasClass("checked")){
										
										$("#singlePrice"+position_i).html((Number($("#singlePrice"+position_i).text())+Number(price)).toFixed(2));

										$("#totalPrice").html((Number($("#totalPrice").text())+Number(price)).toFixed(2));

									}
									
								}else{
									
									$("#cartNum").html(parseInt($("#cartNum").text())-1);
									
									$("#price").html((Number($("#price").text())-Number(price)).toFixed(2));
									
									$("#merNum"+position).html(parseInt($("#merNum"+position).text())-1);
									
									$("#hasBuy_"+merchandiseId+" span:eq(0)").text($("#merNum"+position).text());
									
									if($("#merNum"+position).parent().parent().parent().find(".ldh-checkbox-span").hasClass("checked")){
										
										$("#singlePrice"+position_i).html((Number($("#singlePrice"+position_i).text())-Number(price)).toFixed(2));

										$("#totalPrice").html((Number($("#totalPrice").text())-Number(price)).toFixed(2));

									}
									
									if(nums==0){
										
										$("#cartItemRow"+position).hide();
										
										$("#hasBuy_"+merchandiseId).hide();
										
										$("#sub_"+merchandiseId).hide();
									}
								}
								
								var row_flag=false;
								
								for(var i=0;i<$("#cartItem"+position_i+" dd").length;i++){
									if(!$("#cartItem"+position_i+" dd:eq("+i+")").is(":hidden")){
										row_flag=true;
									}
								}
								
								var cart_flag=false;
								
								for(var i=0;i<$("#cartList dd").length;i++){
									if(!$("#cartList dd:eq("+i+")").is(":hidden")){
										cart_flag=true;
									}
								}
								
								if(!row_flag){
									
									$("#cartItem"+position_i).hide();
								}
								
								if(!cart_flag){
									
									$("#cart_bottom_bar").hide();
									cartNull("#cartList");
								}
								
							}else{
								$("#cartNum").html(nums);
							}
							
							updateFlag=true;
							
							isMinAmount(false);
							
		}))().start();
		
	};
	
	//删除购物车商品
	var deleteFlag=true;
	
	exports.deleteCartItem=function(obj,cartId,merchandiseId,price,position,position_i){
		
		if(!deleteFlag)
			return;
		
		deleteFlag=false;
		
		$(obj).parent().hide();
		
		$(obj).attr("disabled","disabled");
		
		base.showLoading("#cartList");
		
		var params = {
			cartId:cartId
		};
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/deleteCartShop.do", params));

							if(data["code"]==1){
								
								$("#price").html((Number($("#price").text())-(Number(price)*parseInt($("#merNum"+position).text()))).toFixed(2));

								$("#cartNum").html(parseInt($("#cartNum").text())-parseInt($("#merNum"+position).text()));
								
								if($("#merNum"+position).parent().parent().parent().find(".ldh-checkbox-span").hasClass("checked")){
									
									$("#totalPrice").html((Number($("#totalPrice").text())-(Number(price)*parseInt($("#merNum"+position).text()))).toFixed(2));

									$("#singlePrice"+position_i).html((Number($("#singlePrice"+position_i).text())-(Number(price)*parseInt($("#merNum"+position).text()))).toFixed(2));

								}
								
							
								
								var row_flag=false;
								
								for(var i=0;i<$("#cartItem"+position_i+" dd").length;i++){
									if(!$("#cartItem"+position_i+" dd:eq("+i+")").is(":hidden")){
										row_flag=true;
									}
								}
								
								var cart_flag=false;
								
								for(var i=0;i<$("#cartList dd").length;i++){
									if(!$("#cartList dd:eq("+i+")").is(":hidden")){
										cart_flag=true;
									}
								}
								
								if(!row_flag){
									
									$("#cartItem"+position_i).hide();
								}
								
								if(!cart_flag){
									
									$("#cart_bottom_bar").hide();
									cartNull("#cartList");
								}
									
								$("#hasBuy_"+merchandiseId).hide();
								
								$("#sub_"+merchandiseId).hide();
								
								$("#hasBuy_"+merchandiseId+" span:eq(0)").text("0");
								
								
								
							}else{
								
							}
							
							deleteFlag=true;
							
							base.cancelLoading("#cartList");
							
		}))().start();
		
	};
	
	//购物车列表
	exports.getCartList=function(shopId){

		var params = {
				shopId:shopId,
				currentPage:1,
				pageSize:10
			};
		
		base.showLoading("#cartList");
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Cart/getCartShops.do", params));
								if(data["code"]==1){
								
									var htmlStr="",totalPrice=0;
									
									$("#cartList").html(htmlStr);
									
									
									
									for(var i=0;i<data["data"].length;i++){
										
											var logoUrl=data["data"][i]["logoUrl"];
											
											if(logoUrl=="" || logoUrl.indexOf(".")<0){
												
												logoUrl= ctx+'/v1/img/category_f.png';
											}else{
												
												logoUrl=fileOpenUrl+logoUrl;
											}
											
										
										
											var merHtml="";singlePrice=0;
											
											for(var j=0;j<data["data"][i]["goods"].length;j++){
												
												var picdefault=data["data"][i]["goods"][j]["picdefault"];
												
												if(validate.isNull(picdefault)){
													
													picdefault=ctx+"/v1/img/default_234_220.png";
												}else{
													
													picdefault=fileOpenUrl+picdefault;
												}
												
												
												merHtml += '<dd id="cartItemRow'+i+j+'">';
												merHtml += '<table>';
												merHtml += '<colgroup>';
												merHtml += '<col width="50px" />';
												merHtml += '<col width="54px" />';
												merHtml += '<col width="110px" />';
												merHtml += '<col width="75px" />';
												merHtml += '<col width="26px" />';
												merHtml += '<col width="40px" />';
												merHtml += '<col width="54px" />';
												merHtml += '</colgroup>';
												merHtml += '<tbody>';
												merHtml += '<tr>';
												merHtml += '<td><span class="ldh-checkbox-span" onclick=$ldh_cart.selectItem(this,"'+Number(data["data"][i]["goods"][j]["price"])+'",'+i+')><input type="checkbox" name="cartIds" class="opacity_0" value="'+data["data"][i]["goods"][j]["cartId"]+'"  /></span></td>';
												merHtml += '<td><span class="ldh-goods-pic"><img src="'+picdefault+'" /></span></td>';
												merHtml += '<td class="padding_5"><span class="ldh-goods-title">'+data["data"][i]["goods"][j]["merchandiseName"]+'</span></td>';
												merHtml += '<td><span class="ldh-goods-price">¥<b>'+data["data"][i]["goods"][j]["price"]+'</b></span></td>';
												merHtml += '<td class="ldh-goods-operate"><span class="ldh-x">x</span><span class="ldh-sub-btn" onclick=$ldh_cart.updateCartNum("sub","'+data["data"][i]["goods"][j]["cartId"]+'","'+data["data"][i]["goods"][j]["merchandiseId"]+'","'+data["data"][i]["goods"][j]["price"]+'","'+i+j+'",'+i+')></span></td>';
												merHtml += '<td class="text-center"><span class="ldh-goods-lenght" id="merNum'+i+j+'">'+data["data"][i]["goods"][j]["nums"]+'</span></td>';
												merHtml += '<td class="ldh-goods-operate"><span class="ldh-add-btn" onclick=$ldh_cart.updateCartNum("add","'+data["data"][i]["goods"][j]["cartId"]+'","'+data["data"][i]["goods"][j]["merchandiseId"]+'","'+data["data"][i]["goods"][j]["price"]+'","'+i+j+'",'+i+')></span></td>';
												merHtml += '</tr>';
												merHtml += '</tbody>';
												merHtml += '</table>';
												merHtml += '<div class="ldh-order-close" onclick=$ldh_cart.deleteCartItem(this,"'+data["data"][i]["goods"][j]["cartId"]+'","'+data["data"][i]["goods"][j]["merchandiseId"]+'","'+data["data"][i]["goods"][j]["price"]+'","'+i+j+'",'+i+')>x</div>';
												merHtml += '</dd>';
												
												singlePrice+=Number(data["data"][i]["goods"][j]["price"])*parseInt(data["data"][i]["goods"][j]["nums"]);
											}
											
											totalPrice+=singlePrice;
											
											htmlStr += '<dl id="cartItem'+i+'">';
											htmlStr += '<dt style="background-color:#f5f4f2;">';
											htmlStr += '<table>';
											htmlStr += '<colgroup>';
											htmlStr += '<col width="44px" />';
											htmlStr += '<col width="34px" />';
											htmlStr += '<col width="164px" />';
											htmlStr += '<col width="63px" />';
											htmlStr += '</colgroup>';
											htmlStr += '<tbody>';
											htmlStr += '<tr>';
											htmlStr += '<td><span class="ldh-checkbox-span " onclick=$ldh_cart.selectAllItem('+i+')><input type="checkbox" class="opacity_0" /></span></td>';
											htmlStr += '<td><span class="ldh-shop-logo"><a href=javascript:$ldh_base.jumpToShopIndex("'+data["data"][i]["shopId"]+'")><img src="'+logoUrl+'" /></a></span></td>';
											htmlStr += '<td><span class="ldh-shop-name"><a href=javascript:$ldh_base.jumpToShopIndex("'+data["data"][i]["shopId"]+'")>'+data["data"][i]["shopName"]+'</a></span><span class="ldh-goods-num"></span></td>';
											//htmlStr += '<td class="text-center"><span class="ldh-cart-tips" id="tip_amount"></span></td>';
											//htmlStr += '<td class="text-right"><button type="button" class="ldh-closing-btn ldh-bg-yellow">¥<b id="singlePrice'+i+'">'+singlePrice.toFixed(2)+'</b><b style="display:none;">'+data["data"][i]["minAmount"]+'</b></button></td>';
											htmlStr += '<td class="text-right"><button type="button" class="ldh-closing-btn ldh-bg-yellow">¥<b id="singlePrice'+i+'">0.00</b><b style="display:none;">'+data["data"][i]["minAmount"]+'</b></button></td>';

											htmlStr += '</tr>';
											htmlStr += '</tbody>';
											htmlStr += '</table>';
											htmlStr += '</dt>';
											htmlStr +=merHtml;
											htmlStr += '<dd class="ldh-recess-tips" id="tip_businesss" style="display:none;">';
											//htmlStr += '<span>温馨提示:店铺休息中,暂停服务.</span><span class="allowAddOrder" style="display:none;">'+data["data"][i]["allowAddOrder"]+'</span>';
											htmlStr += '<span>'+data["data"][i]["allowAddOrder"]+'</span>';
											htmlStr += '</dd>';
											htmlStr += '</dl>';
										
									}
								
									$("#cartList").html(htmlStr);
									$("#totalPrice").html("0.00");
									//$("#totalPrice").html(totalPrice.toFixed(2));
									
									/*if($("#business_Status").val()!="1"){
										
										$("#tip_businesss").show();
										
										$("#btn_settle").attr("disabled","disabled");
									}*/
									
								}else{
									
									
									
								}
								base.cancelLoading("#cartList");
								
								if(data["data"].length==0){
									cartNull("#cartList");
									$("#cart_bottom_bar").hide();
								}else{
									re_cartNull("#cartList");
									$("#cart_bottom_bar").show();
								}
								
			}))().start();

	};

	//选择商家的全部商品
	exports.selectAllItem=function(position){
		$("#tip_amount").html("");
		if($("#cartItem"+position+" .ldh-checkbox-span").hasClass("checked")){
			
			$("#cartItem"+position+" .ldh-checkbox-span").removeClass("checked");
			$("#cartItem"+position+" .ldh-checkbox-span").children().attr("checked",false);
			
			$("#totalPrice").html((Number($("#totalPrice").text())-Number($("#singlePrice"+position).text())).toFixed(2));
			
			//$("#price").html((Number($("#totalPrice").text())-Number($("#singlePrice"+position).text())).toFixed(2));
			$("#singlePrice"+position).html("0.00");
			
		}else{
		
			$("#cartItem"+position+" .ldh-checkbox-span").addClass("checked");
			$("#cartItem"+position+" .ldh-checkbox-span").children().attr("checked",true);
			
			var _price=0;
			
			$("#cartItem"+position+" .ldh-checkbox-span").each(function(index){
				if(index>0){
					_price+=Number($("#cartItem"+position+" .ldh-goods-price:eq("+(index-1)+") b").text())*parseInt($("#cartItem"+position+" .ldh-goods-lenght:eq("+(index-1)+")").text());
				}
			});
			
			$("#totalPrice").html((Number($("#totalPrice").text())+Number(_price)).toFixed(2));
			
			//$("#price").html((Number($("#totalPrice").text())+Number(_price)).toFixed(2));
			
			$("#singlePrice"+position).html(Number(_price).toFixed(2));
			
			
			if(!validate.isNull($("#cartList dl:eq("+position+") b:eq(1)").text()) && $("#cartList dl:eq("+position+") .ldh-checkbox-span").hasClass("checked") && $("#cartList dl:eq("+position+")").is(":visible")){
				
				var curAmount=Number($("#cartList dl:eq("+position+") b:eq(0)").text());
				
				var minAmount=Number($("#cartList dl:eq("+position+") b:eq(1)").text());
				
				var curShopName=$("#cartList dl:eq("+position+") .ldh-shop-name").text();
				
				if(curAmount<minAmount){
					
					$("#tip_amount").html("'"+curShopName+"'还差"+(Number(minAmount)-Number(curAmount)).toFixed(2)+"元免配送费");
					
				}
				
			}

		}
		
		
		
	};
	
	//选择某个商品
	exports.selectItem=function(obj,price,position){
			$("#tip_amount").html("");
			var out_price=Number(price)*parseInt($(obj).parent().parent().parent().find(".ldh-goods-lenght").text());
				
			if($(obj).hasClass("checked")){
				
				$(obj).removeClass("checked");	
				
				$(obj).children().attr("checked",false);
				
				$("#singlePrice"+position).html((Number($("#singlePrice"+position).text())-Number(out_price)).toFixed(2));

				$("#totalPrice").html((Number($("#totalPrice").text())-Number(out_price)).toFixed(2));
				
				//$("#price").html((Number($("#totalPrice").text())-Number(price)).toFixed(2));

				
			}else{
				
				$(obj).addClass("checked");
				
				$(obj).children().attr("checked",true);
				
				$("#singlePrice"+position).html((Number($("#singlePrice"+position).text())+Number(out_price)).toFixed(2));
				
				$("#totalPrice").html((Number($("#totalPrice").text())+Number(out_price)).toFixed(2));
				
				//$("#price").html((Number($("#totalPrice").text())+Number(price)).toFixed(2));
				
				if(!$("#cartItem"+position+" dt .ldh-checkbox-span").hasClass("checked")){
					
					$("#cartItem"+position+" dt .ldh-checkbox-span").addClass("checked");
				}
				
					
					
					
				
			}	
			
			/*if($("#singlePrice"+position).text()=="0.00"){
				
				$("#cartItem"+position+" dt .ldh-checkbox-span").addClass("checked");
			}*/
			var flag=true;
			
			for(var i=1;i<$("#cartItem"+position+" .ldh-checkbox-span").length;i++){
				
				if($("#cartItem"+position+" .ldh-checkbox-span:eq("+i+")").hasClass("checked")){
					//表示购物车中的某个商家还有商品是选中的 
					flag=false;
				}
				
			}
			
			//如果全部商品都没有选中,那就把商家左边的勾去掉
			if(flag){
				
				$("#cartItem"+position+" dt .ldh-checkbox-span").removeClass("checked");
			}
			
			/*if(!$("#cartItem"+position+" .ldh-checkbox-span").hasClass("checked")){
				
			}*/
			isMinAmount(false);
	};
	
	//结算
	
	var curShopName="";
	
	exports.settle=function(){
	
		if(!base.validateLoginStatusByUserId()){
			
			base.loginShow();
			
			return;
		}
		
		if(!isMerNull()){
			
			$("#tip_amount").html("请选择商品");
			
			return;
			
		}
		
		if(!isBusinessStatus()){
			
			$("#tip_amount").html("'"+curShopName+"'休息中,暂停服务");
			
			return;
		}
		
		
		var shopNum=0;
		
		for(var i=0;i<$("#cartList dl").length;i++){
			
			if($("#cartList dl:eq("+i+")").find(".ldh-checkbox-span").hasClass("checked") && $("#cartList dl:eq("+i+")").is(":visible")){
				
				shopNum=shopNum+1;
				
			}
		}
		
		if(shopNum>1){
			
			$("#tip_amount").html("只能选择一个商家进行结算");
			
			return;
		}

		isMinAmount(true);
		
		
	};
	
	
	
	//判断是否选择了商品
	function isMerNull(){
		
		var flag=false;
		
		for(var i=0;i<$("#cartList dl").length;i++){
			
			if($("#cartItem"+i+" dt .ldh-checkbox-span").hasClass("checked")){
				
				flag=true;
			}
			
		}
		
		return flag;
	}
	
	
	//判断商家营业状态
	function isBusinessStatus(){
		
		//判断购物车中的店铺列表是否存在某一间是休息中,如有有的，则不能购买
		var businessStatusFlag=true;

		for(var i=0;i<$("#cartList dl").length;i++){
			
			
			if($("#cartList dl:eq("+i+") .ldh-checkbox-span").hasClass("checked") && $("#cartList dl:eq("+i+")").is(":visible")){
				
				if(Number($("#cartList dl:eq("+i+") .ldh-recess-tips span").text())!=1){
					
					businessStatusFlag=false;
					
					curShopName=$("#cartList dl:eq("+i+") .ldh-shop-name").text();
					
					break;
				}
				
			}
			
			
		}
		
		
		return businessStatusFlag;
		
	}
	
	//判断起送金额是否足够
	function isMinAmount(flag){
		
		//判断起送金额是否足够
		var minAmountFlag=true,minAmount=0,curAmount=0;
		
		for(var i=0;i<$("#cartList dl").length;i++){
			
			if(!validate.isNull($("#cartList dl:eq("+i+") b:eq(1)").text()) && $("#cartList dl:eq("+i+") .ldh-checkbox-span").hasClass("checked") && $("#cartList dl:eq("+i+")").is(":visible")){
				
				if(Number($("#cartList dl:eq("+i+") b:eq(0)").text())<Number($("#cartList dl:eq("+i+") b:eq(1)").text())){
					
					minAmountFlag=false;
					
					curAmount=$("#cartList dl:eq("+i+") b:eq(0)").text();
					
					minAmount=$("#cartList dl:eq("+i+") b:eq(1)").text();
					
					curShopName=$("#cartList dl:eq("+i+") .ldh-shop-name").text();
					
					break;
				}
				
			}
			
		}
		
		//alert(minAmountFlag);
		if(minAmountFlag){
			
				
				$("#tip_amount").html("");
				
				if(!flag)
					return;
			
				var cartId="";
				
				var arr_cart_id = new Array();
				
				$(".ldh-checkbox-span input[name='cartIds'][type='checkbox']").each(function(){
					if($(this).is(":checked")){
						arr_cart_id.push($(this).val());
					}
				});
				
				cartId= arr_cart_id.join(",");
				
				//跳转到结算页面
				//top.location=ctx+"/suborder.jsp?cartId="+cartId;
				
				window.location.href=(encodeURI(host+ctx+"/v1/pages/order/suborder.jsp?"+base.encodeParams("cartId="+cartId)));

				
		}else{
			
			
			$("#tip_amount").html("'"+curShopName+"'还差"+(Number(minAmount)-Number(curAmount)).toFixed(2)+"元免配送费");
			
			if(flag){
				
				var cartId="";
				
				var arr_cart_id = new Array();
				
				$(".ldh-checkbox-span input[name='cartIds'][type='checkbox']").each(function(){
					if($(this).is(":checked")){
						arr_cart_id.push($(this).val());
					}
				});
				
				cartId= arr_cart_id.join(",");
				
				//跳转到结算页面
				//top.location=ctx+"/suborder.jsp?cartId="+cartId;
				
				window.location.href=(encodeURI(host+ctx+"/v1/pages/order/suborder.jsp?"+base.encodeParams("cartId="+cartId)));
			}
		}
		
	}
	
	//购物车为空
	function cartNull(divId){
			var cartnull='<div class="ldh-cart-null"><p>亲，购物车暂无商品，赶紧购物吧！</p></div>';
			$(divId).append(cartnull);
	}
	
	//购物车不为空
	function re_cartNull(divId){
			$(divId).find(".ldh-cart-null").remove();
	}
	
});